package com.baselib.libnetwork.net.adapter;


import com.baselib.libnetwork.net.base.Callback;
import com.baselib.libnetwork.net.model.ResultResponse;
import com.baselib.libnetwork.net.policy.HttpPolicy;
import com.baselib.libnetwork.net.policy.OkhttpPolicy;
import com.baselib.libnetwork.net.request.CustomRequest;

/**
 * @author lixuce
 * @version v1.0
 * @description
 * @since 2017/10/29
 */

public class OkHttpCall<T> implements Call<T> {
    private HttpPolicy<T> policy;
    private CustomRequest<T, ? extends CustomRequest> request;

    public OkHttpCall(CustomRequest<T, ? extends CustomRequest> request) {
        this.request = request;
        policy = new OkhttpPolicy(request);
    }

    @Override
    public ResultResponse<T> execute() throws Exception {
        return policy.requestSync();
    }

    @Override
    public void execute(final Callback<T> callback) {
        policy.requestAsync(callback);
    }

    @Override
    public void cancel() {
        policy.cancel();
    }

    @Override
    public boolean isCanceled() {
        return policy.isCanceled();
    }

    @Override
    public Call<T> clone() {
        return new OkHttpCall<>(request);
    }

    @Override
    public CustomRequest getRequest() {
        return request;
    }
}
